$(function () {
    var listUrl = '../toolscheckjudgestandard/list';
    var toolsFileId = getQueryString('toolsFileId');
    if(toolsFileId){
        listUrl += '?toolsFileId=' + toolsFileId;
        vm.toolsFileId = toolsFileId;
    }
    $("#jqGrid").Grid({
        url: listUrl,
        colModel: [
            {label: '检测周期', index: 'CHECK_PERIOD',
                formatter: function (value, col, row) {
                    let checkPeriodDesc = '未知';
                    if(row.checkPeriod == 1){
                        checkPeriodDesc = '1次/月';
                    } else if(row.checkPeriod == 3){
                        checkPeriodDesc = '1次/季度';
                    } else if(row.checkPeriod == 6){
                        checkPeriodDesc = '1次/半年';
                    } else if(row.checkPeriod == 12){
                        checkPeriodDesc = '1次/年';
                    }
                    return checkPeriodDesc;
                }
            },
            {label: '测量工具', name: 'checkTool', index: 'CHECK_TOOL'},
            {label: '判定标准', name: 'judgeStandard', index: 'JUDGE_STANDARD'},
            {label: '工装测量点', name: 'position', index: 'POSITION'},
            {label: '最大值', name: 'startInterval', index: 'START_INTERVAL'},
            {label: '最小值', name: 'endInterval', index: 'END_INTERVAL'},
            {label: '备注', name: 'remark', index: 'REMARK'},
            {label: '工装档案ID', name: 'toolsFileId', hidden: true},
            {label: '检测周期', name: 'checkPeriod', hidden: true},
            {label: 'id', name: 'id', key: true, hidden: true},
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        toolsFileId: '', // 工装档案ID
        toolsCheckJudgeStandard: {
            remark: '',
            endInterval: '',
            startInterval: '',
            judgeStandard: '',
            checkTool: '',
            checkPeriod: '',
            toolsFileId: '',
            position:'',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            judgeStandard: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.toolsCheckJudgeStandard = {
                remark: '',
                endInterval: '',
                startInterval: '',
                judgeStandard: '',
                position:'',
                checkTool: '',
                checkPeriod: '',
                toolsFileId: '',
                id: '',
            };
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url;
            if(vm.toolsCheckJudgeStandard.id == (null || '')){
                url = "../toolscheckjudgestandard/save";
                vm.toolsCheckJudgeStandard.toolsFileId = vm.toolsFileId;
            }else{
                url = "../toolscheckjudgestandard/update";
            }
            // vm.toolsCheckJudgeStandard.checkPeriod = vm.toolsCheckJudgeStandard["checkPeriod+''"];
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.toolsCheckJudgeStandard),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../toolscheckjudgestandard/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../toolscheckjudgestandard/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.toolsCheckJudgeStandard = r.toolsCheckJudgeStandard;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'judgeStandard': vm.q.judgeStandard},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                judgeStandard: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});
